package com.sservice.service.service.impl;

import com.sservice.common.Constants;
import com.sservice.urp.dao.SysLogMapper;
import com.sservice.urp.entity.SysLog;
import com.sservice.urp.entity.User;
import com.sservice.model.log.Action;
import com.sservice.urp.vo.SysLogVo;
import com.sservice.service.service.ISysLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by suweiming on 2017/12/20.
 *
 */
@Service
class SysLogServiceImpl
        implements ISysLogService {
    @Autowired
    public SysLogMapper dao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SysLog record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(SysLog record) {
        return dao.insertSelective(record);
    }

    @Override
    public SysLog selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }
    public int updateByPrimaryKeySelective(SysLog record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(SysLog record) {
        return dao.updateByPrimaryKey(record);
    }

    public int queryByCount(SysLogVo record) {
        return dao.queryByCount(record);
    }

    @Override
    public List<SysLogVo> queryByPage(SysLogVo record) {
        return dao.queryByPage(record);
    }

    /**
     * 通过日志类型返回类型字符串
     *
     * @param type
     * @return
     */
    private String getTypeStr(int type) {
        String typeStr = "";
        switch (type) {
            case 1:
                typeStr = "Com";
                break;
            case 2:
                typeStr = "Dep";
                break;
            case 3:
                typeStr = "User";
                break;
            case 4:
                typeStr = "Login";
                break;
            case 5:
                typeStr = "Ads";
                break;
            case 6:
                typeStr = "Info";
                break;
            case 7:
                typeStr = "Goods";
                break;
            case 8:
                typeStr = "Offer";
                break;
            case 9:
                typeStr = "Order";
                break;
            case 10:
                typeStr = "Role";
                break;
            default:
        }
        return typeStr;
    }

    @Override
    public void log(Object content, HttpServletRequest request, Integer objectId, int type) {
        try {
            SysLog log = new SysLog();
            if (content instanceof Map) {
//            Method m = this.getClass().getDeclaredMethod("to" + getTypeStr(type) + "String");
//            Object res = m.invoke(this);
                if (type == Action.comType) {
                    log.setContent(this.toComString(content));
                } else if (type == Action.depType) {
                    log.setContent(this.toDepString(content));
                } else if (type == Action.userType) {
                    log.setContent(this.toUserString(content));
                } else if (type == Action.goodsType) {
                    log.setContent(this.toGoodsString(content));
                } else if (type == Action.offerType) {
                    log.setContent(this.toOfferString(content));
                } else if (type == Action.payTypeType) {
                    log.setContent(this.toPayTypeString(content));
                }
            } else {
                log.setContent(content.toString());
            }
            if (log.getContent().length() == 0) {
                return;
            }
            log.setObjectId(objectId);
            User user = (User) request.getSession().getAttribute(Constants.USER);
            log.setOperator(user.getId());
            log.setIp(Constants.getIP(request)); // 获取IP地址
            log.setType(type);
            dao.insertSelective(log);
        } catch (Exception e) {
            logger.error("【日志】记录日志异常" + e.getMessage());
        }
    }

    /**
     * 单位 变化字符串
     *
     * @param obj
     * @return
     */
    public String toComString(Object obj) {
        Map fieldKeyValue = new HashMap();
        fieldKeyValue.put("tel", "电话");
        fieldKeyValue.put("fax", "传真");
        fieldKeyValue.put("address", "地址");
        fieldKeyValue.put("bank", "开户行");
        fieldKeyValue.put("bankAccount", "开户行账户");
        fieldKeyValue.put("taxNum", "税号");
        List<String> strArr = new ArrayList<String>();
        Map map = (Map) obj;
        for (Object str : map.keySet()) {
            if (fieldKeyValue.get(str) != null && fieldKeyValue.get(str) != null) {
                strArr.add(fieldKeyValue.get(str).toString() + map.get(str).toString());
            }
        }
        return StringUtils.join(strArr, ",");
    }

    public String toDepString(Object obj) {
        Map fieldKeyValue = new HashMap();
        fieldKeyValue.put("tel", "电话");
        fieldKeyValue.put("fax", "传真");
        List<String> strArr = new ArrayList<String>();
        Map map = (Map) obj;
        for (Object str : map.keySet()) {
            strArr.add(fieldKeyValue.get(str).toString() + map.get(str).toString());
        }
        return StringUtils.join(strArr, ",");
    }

    public String toUserString(Object obj) {
        Map fieldKeyValue = new HashMap();
        fieldKeyValue.put("userName", "用户名");
        fieldKeyValue.put("gender", "性别");
        fieldKeyValue.put("qq", "QQ");
        fieldKeyValue.put("phone", "手机");
        List<String> strArr = new ArrayList<String>();
        Map map = (Map) obj;
        for (Object str : map.keySet()) {
            strArr.add(fieldKeyValue.get(str).toString() + map.get(str).toString());
        }
        return StringUtils.join(strArr, ",");
    }

    public String toGoodsString(Object obj) {
        Map fieldKeyValue = new HashMap();
        fieldKeyValue.put("desc", "描述");
        fieldKeyValue.put("taxRatio", "税率");
        fieldKeyValue.put("code", "编码");
        fieldKeyValue.put("order", "排序号");
        fieldKeyValue.put("unit", "计量单位");
        fieldKeyValue.put("online", "上线状态");
        fieldKeyValue.put("isDanger", "是否危险品");
        fieldKeyValue.put("isDrugs", "是否易制毒");
        fieldKeyValue.put("operatorId", "运营人员");
        List<String> strArr = new ArrayList<String>();
        Map map = (Map) obj;
        for (Object str : map.keySet()) {
            strArr.add(fieldKeyValue.get(str).toString() + map.get(str).toString());
        }
        return StringUtils.join(strArr, ",");
    }

    public String toOfferString(Object obj) {
        Map fieldKeyValue = new HashMap();
        fieldKeyValue.put("desc", "描述");
        fieldKeyValue.put("taxRatio", "税率");
        fieldKeyValue.put("code", "编码");
        fieldKeyValue.put("order", "排序号");
        fieldKeyValue.put("unit", "计量单位");
        fieldKeyValue.put("online", "上线状态");
        fieldKeyValue.put("isDanger", "是否危险品");
        fieldKeyValue.put("isDrugs", "是否易制毒");
        fieldKeyValue.put("operatorId", "运营人员");
        List<String> strArr = new ArrayList<String>();
        Map map = (Map) obj;
        for (Object str : map.keySet()) {
            String str_ = "";
            if (fieldKeyValue.get(str) != null) {
                str_ += fieldKeyValue.get(str).toString();
            }
            if (map.get(str) != null) {
                str_ += map.get(str).toString();
            }
            strArr.add(str_);
        }
        return StringUtils.join(strArr, ",");
    }

    public String toPayTypeString(Object obj) {
        Map fieldKeyValue = new HashMap();
        fieldKeyValue.put("remark", "备注");
        fieldKeyValue.put("name", "账户名称");
        fieldKeyValue.put("order", "排序号");
        List<String> strArr = new ArrayList<String>();
        Map map = (Map) obj;
        for (Object str : map.keySet()) {
            strArr.add(fieldKeyValue.get(str).toString() + map.get(str).toString());
        }
        return StringUtils.join(strArr, ",");
    }


}
